﻿using Authentication.Model.Interface;
using Authentication.Model.Model;
using Common.Contract;
using Common.Contract.Authentication.Request;
using Common.Data.Repository.Enum;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Core.ServiceCommand
{
    public class UpdateRequestDaoByGuestServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateCreateDaoRequestRequest, ResponseRecord>
    {
        public UpdateRequestDaoByGuestServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(UpdateCreateDaoRequestRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.Record == null)
            {
                throw new ArgumentException("Record must be set", "request");
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var record = Request.Record;
            var userQuestion = new CreateDaoRequest
            {
                Pic = record.Pic,
                BranchId = record.BranchId,
                CreatedDate = DateTime.Now,
                FullName = record.FullName,
                Email = record.Email,
                Cif = record.Cif,
                Status = (int)StatusRequestCreateDaoEnum.Request,
                LegalId = record.LegalId,
                LinkOfferLetter = record.PathOfferLetter,
                PositionName = record.PositionName,
                StaffId = record.StaffId,
                Phone = record.Phone
            };

            try
            {
                bool savedUser = Providers.RequestDao.SaveDaoRequest(userQuestion);
                response.IsSuccess = savedUser;
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
                response.IsSuccess = false;
            }
            return response.IsSuccess;
        }
    }
}
